package com.wd.tech;

import android.content.Context;
import android.content.Intent;
import android.graphics.Outline;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.stx.xhb.androidx.XBanner;
import com.wd.tech.Api.Api;
import com.wd.tech.Base.BaseActivity;
import com.wd.tech.Base.BasePresenter;
import com.wd.tech.Bean.News_Show_Bean;
import com.wd.tech.Bean.XBannerBean;
import com.wd.tech.activity.NewsMoreActivity;
import com.wd.tech.adapter.NewShowAdapter;
import com.wd.tech.contracts.Contract;
import com.wd.tech.mvp.Presenter;
import com.wd.tech.net.NetGlide;
import com.wd.tech.net.NetUtlis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {


    @BindView(R.id.text_zx)
    TextView textZx;
    @BindView(R.id.Search_for)
    ImageView SearchFor;
    private List<XBannerBean.ResultBean> list = new ArrayList<>();
    private boolean wang;
    private XBanner xbanner;
    private ImageView news;
    private RecyclerView shownew;
    private NewShowAdapter newShowAdapter;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initData() {

        //判断网络
        wang = NetUtlis.getInstance().isWang();
        //查询banner
        mPresenter.PresenterGetXBannerInfo(Api.XBannerUrl, XBannerBean.class);
        //展示资讯首页、
        Map<String ,Object> show = new HashMap<>();
        show.put("plateId",1);
        show.put("page",2);
        show.put("count",5);
        mPresenter.PresenterGetNewsShow(Api.News_Sh,show,News_Show_Bean.class);
    }

    @Override
    protected BasePresenter initPresenter() {
        return new Presenter();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initViews() {

        xbanner = findViewById(R.id.xxbanner);
        news = findViewById(R.id.News_More);
        shownew = findViewById(R.id.show_news);

        news.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, NewsMoreActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected int initLaout() {
        return R.layout.activity_main;
    }

    @Override
    public void onSucces(Object o) {

        if (wang) {
            if (((XBannerBean) o).getStatus().equals("0000")) {
                list.clear();
                list.addAll(((XBannerBean) o).getResult());
                //设置XBanner数据
                xbanner.setBannerData(list);
                //设置适配器
                xbanner.loadImage(new XBanner.XBannerAdapter() {
                    @Override
                    public void loadBanner(XBanner banner, Object model, View view, int position) {
                        //设置数据
                        NetGlide.GlideImage(list.get(position).getImageUrl(), (ImageView) view);
                        //设置XBanner自动滑动速度
                        xbanner.setPageChangeDuration(1000);
                    }
                });
            }
            if (o instanceof News_Show_Bean){
                if (((News_Show_Bean)o).getResult().equals("0000")){
                    newShowAdapter = new NewShowAdapter(MainActivity.this,((News_Show_Bean)o).getResult());
                    shownew.setAdapter(newShowAdapter);
                    newShowAdapter.notifyDataSetChanged();
                }
            }
        }else {
            Toast.makeText(this, "无网....请先检查网络是否异常", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onError(String error) {

    }
}
